import { http } from '@/utils/http'
import { useWorkmainStore, useWorksubStore, useUserStore, useWorktoolStore } from '@/stores'
function isNetWork() {
  uni.getNetworkType({
    success: function (res) {
      if (res.networkType === 'none') {
        uni.showToast({
          title: '无网络',
          icon: 'none',
        })
        return false
      }
      return true
    },
  })
}
// 登录
export function apiGetToken(loginData) {
  return http({
    method: 'POST',
    url: '/web/session/authenticate',
    data: {
      db: 'odoo16-test',
      ...loginData,
    },
  })
}
// export async function apiGetWorkmain(companies) {
//获取main数据
// export function apiGetWorkmain(companies) {
export function apiGetWorkmain(uid) {
  // console.log('u', [uid])
  const workmainStore = useWorkmainStore()
  if (!isNetWork()) {
    // const res = await http({
    return http({
      method: 'POST',
      url: '/web/dataset/call_kw',
      data: {
        model: 'meas.work.main',
        method: 'web_search_read',
        args: [],
        kwargs: {
          limit: 0,
          offset: 0,
          order: '',
          // "context": {
          // 	"lang": "en_US",
          // 	"tz": "Asia/Shanghai",
          // 	"uid":uid,
          // 	"allowed_company_ids": [],
          // 	"bin_size": true
          // },
          count_limit: 10001,
          fields: [],
          // domain: [['company_id', 'in', companies]],
          domain: [['permit_user_ids', 'in', uid]],
        },
      },
    })
    // workmainStore.setRecords(res.data.result.records)
  }
  return workmainStore.records
}

// export async function apiGetWorksub(work_main_id) {
//获取sub 数据
export function apiGetWorksub(work_main_id) {
  const worksubStore = useWorksubStore()
  if (!isNetWork()) {
    // const res = await http({
    return http({
      method: 'POST',
      url: '/web/dataset/call_kw',
      data: {
        model: 'meas.work.sub',
        method: 'web_search_read',
        args: [],
        kwargs: {
          limit: 0,
          offset: 0,
          order: '',
          // "context": {
          // 	"lang": "en_US",
          // 	"tz": "Asia/Shanghai",
          // 	"uid":uid,
          // 	"allowed_company_ids": [],
          // 	"bin_size": true
          // },
          count_limit: 10001,
          fields: [],
          domain: [['work_main_id', '=', work_main_id]],
        },
      },
    })
    // console.log(res.data.result.records)

    // worksubStore.setRecords(work_main_id, res.data.result.records)
  }
  return worksubStore.records[work_main_id]
}
export function apiGetPassUsers() {
  const userStore = useUserStore()
  if (!isNetWork()) {
    // const res = await http({
    return http({
      method: 'POST',
      url: '/web/dataset/call_kw',
      data: {
        model: 'res.groups',
        method: 'web_search_read',
        args: [],
        kwargs: {
          limit: 0,
          offset: 0,
          order: '',
          // "context": {
          // 	"lang": "en_US",
          // 	"tz": "Asia/Shanghai",
          // 	"uid":uid,
          // 	"allowed_company_ids": [],
          // 	"bin_size": true
          // },
          count_limit: 10001,
          fields: ['users'],
          domain: [['name', '=', '放行']],
        },
      },
    })
    // workmainStore.setRecords(res.data.result.records)
  }
  return userStore.users
}
export function apiGetSelfChUsers() {
  const userStore = useUserStore()
  if (!isNetWork()) {
    // const res = await http({
    return http({
      method: 'POST',
      url: '/web/dataset/call_kw',
      data: {
        model: 'res.groups',
        method: 'web_search_read',
        args: [],
        kwargs: {
          limit: 0,
          offset: 0,
          order: '',
          // "context": {
          // 	"lang": "en_US",
          // 	"tz": "Asia/Shanghai",
          // 	"uid":uid,
          // 	"allowed_company_ids": [],
          // 	"bin_size": true
          // },
          count_limit: 10001,
          fields: ['users'],
          domain: [['name', '=', '自选']],
        },
      },
    })
    // workmainStore.setRecords(res.data.result.records)
  }
  return userStore.users
}
//上传数据
export function apiPutMeasData(value_list) {
  if (!isNetWork()) {
    // const res = await http({
    return http({
      method: 'POST',
      url: '/web/dataset/call_kw',
      data: {
        model: 'meas.data',
        method: 'create',
        args: [value_list],
        kwargs: {
          // "context": {
          // 	"lang": "en_US",
          // 	"tz": "Asia/Shanghai",
          // 	"uid":uid,
          // 	"allowed_company_ids": [],
          // 	"bin_size": true
          // },
        },
      },
    })
    // workmainStore.setRecords(res.data.result.records)
  }
}
//获取服务器工具信息
export function apiGetTools() {
  if (!isNetWork()) {
    // const res = await http({
    return http({
      method: 'POST',
      url: '/web/dataset/call_kw',
      data: {
        model: 'meas.tool.info',
        method: 'web_search_read',
        args: [],
        kwargs: {
          limit: 0,
          offset: 0,
          order: '',
          // "context": {
          // 	"lang": "en_US",
          // 	"tz": "Asia/Shanghai",
          // 	"uid":uid,
          // 	"allowed_company_ids": [],
          // 	"bin_size": true
          // },
          count_limit: 10001,
          fields: ['deviceId', 'partno', 'key'],
          domain: [['deviceId', '!=', false]],
        },
      },
    })
    // console.log(res.data.result.records)

    // worksubStore.setRecords(work_main_id, res.data.result.records)
  }
  return useWorktoolStore.tools
}
//新增工具信息记录
export function apiPutToolsData(value_list) {
  if (!isNetWork()) {
    // const res = await http({
    return http({
      method: 'POST',
      url: '/web/dataset/call_kw',
      data: {
        model: 'meas.tool.info',
        method: 'create',
        args: [value_list],
        kwargs: {
          // "context": {
          // 	"lang": "en_US",
          // 	"tz": "Asia/Shanghai",
          // 	"uid":uid,
          // 	"allowed_company_ids": [],
          // 	"bin_size": true
          // },
        },
      },
    })
    // workmainStore.setRecords(res.data.result.records)
  }
}
//修改服务器工具信息
export function apiWriteToolData(id, vals) {
  if (!isNetWork()) {
    // const res = await http({
    return http({
      method: 'POST',
      url: '/web/dataset/call_kw',
      data: {
        model: 'meas.tool.info',
        method: 'write',
        args: [id, vals],
        kwargs: {
          // "context": {
          // 	"lang": "en_US",
          // 	"tz": "Asia/Shanghai",
          // 	"uid":uid,
          // 	"allowed_company_ids": [],
          // 	"bin_size": true
          // },
        },
      },
    })
    // workmainStore.setRecords(res.data.result.records)
  }
}
